<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;
use util\Tree;

/**
 * 商品管理
 * @package app\erp\model
 */
class ErpGoods extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_goods';

    // 定义全局的查询范围
    protected function base($query)
    {
        $query->where('goods_status', 0);
    }

    /**
     * 添加数据
     * @param goods_name 	string 商品名称
     * @param goods_code 	string 商品编码
     * @param supplier_id 	int    供应商id
     * @param brand_id 	    int    品牌id
     * @param unit_measurement 	string 计量单位
     * @param is_single_specification 	    int    是否多规格
     * @param user_id 	    int    用户
     * @author jason
     * @return array_out(code, message, (int)id)
     */
    public function addData($goods_name, $goods_code, $supplier_id, $brand_id, $unit_measurement, $is_single_specification, $user_id, $goods_classtempid){
        $data['goods_name']                 = $goods_name;
        //$data['goods_classid']              = $goods_classid;
        $data['goods_code']                 = $goods_code;
        $data['supplier_id']                = $supplier_id;
        $data['brand_id']                   = $brand_id;
        $data['unit_measurement']           = $unit_measurement;
        $data['is_single_specification']    = $is_single_specification;
        $data['uid']                        = $user_id;
        $data['goods_classtempid']          = $goods_classtempid;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 供应商信息 关联载入（一对一）
     */
    public function getErpSupplier(){
        return $this -> hasOne('app\erp\model\ErpSupplier','id', 'supplier_id');
    }

    /**
     * 品牌信息 关联载入（一对一）
     */
    public function getErpGoodsBrand(){
        return $this -> hasOne('app\erp\model\ErpGoodsBrand','id', 'brand_id');
    }

    /**
     * 订单 单品 关联载入（一对多）
     */
    public function getErpGoodsOrderSkulist(){
        return $this -> hasMany('app\erp\model\ErpGoodsOrderSkulist','order_id', 'id');
    }
}
